package com.sc.nft.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.FilterIncomeListDao;
import com.sc.nft.dao.SandTransferRecordDao;
import com.sc.nft.dao.UserWalletDao;
import com.sc.nft.entity.*;
import com.sc.nft.enums.ExpenditureRecordTypeEnum;
import com.sc.nft.enums.PayChannelEnum;
import com.sc.nft.enums.WalletChannelEnum;
import com.sc.nft.enums.WalletDetailTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.SandTransferRecordService;
import com.sc.nft.service.StaticDataService;
import com.sc.nft.service.UserWalletService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.util.OrderNoUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 衫德转帐记录表服务接口实现
 *
 * @author zxy
 * @since 2023-01-01 18:54:35
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SandTransferRecordServiceImpl implements SandTransferRecordService {
    private final SandTransferRecordDao sandTransferRecordDao;
    private final UserWalletDao userWalletDao;
    private final UserWalletService userWalletService;
    private final StaticDataService staticDataService;
    private final FilterIncomeListDao filterIncomeListDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SandTransferRecord transfer(UserInfo userInfo, BigDecimal amount, WalletChannelEnum channel) {
        if (filterIncomeListDao.getIsContainsUserId(userInfo.getId())) {
            throw new GlobalRunTimeException(ErrorCode.MARKET_VALUE_ACCOUNT_IS_PROHIBITED_TO_WITHDRAW_CASH_TRANSFER);
        }
        if (amount.compareTo(Convert.toBigDecimal(0))<1) {
            throw new GlobalRunTimeException("转账金额必须大于0元");
        }
        if (NumberUtil.isGreater(amount,Convert.toBigDecimal(49999))  ) {
            throw new GlobalRunTimeException("转账金额必须小于50000元");
        }

        StaticData byType = staticDataService.getByType("transfer_accounts_min");

        BigDecimal bigDecimal = Convert.toBigDecimal(byType.getValue());
        if (!(amount.compareTo(bigDecimal)==1||amount.compareTo(bigDecimal)==0)) {
            throw new GlobalRunTimeException("额度必须大于"+byType.getValue()+"元");
        }
        WalletDetailTypeEnum detailTypeEnum = null;
        ExpenditureRecordTypeEnum expenditureRecordTypeEnum = null;
        switch (channel) {

            case SAND:
                detailTypeEnum = WalletDetailTypeEnum.TRANSFER_SAND;
                expenditureRecordTypeEnum = ExpenditureRecordTypeEnum.TRANSFER_SAND;
                break;
            case HF:
                detailTypeEnum = WalletDetailTypeEnum.TRANSFER_HUIFU;
                expenditureRecordTypeEnum = ExpenditureRecordTypeEnum.TRANSFER_HUIFU;
                break;
            case LL:
                detailTypeEnum = WalletDetailTypeEnum.TRANSFER_LL;
                expenditureRecordTypeEnum = ExpenditureRecordTypeEnum.TRANSFER_LL;
                break;
            case HNA:
                detailTypeEnum = WalletDetailTypeEnum.TRANSFER_HNA;
                expenditureRecordTypeEnum = ExpenditureRecordTypeEnum.TRANSFER_HNA;
                break;
        }
        userWalletService.transfer(userInfo.getId(),amount, detailTypeEnum,expenditureRecordTypeEnum);
        SandTransferRecord sandTransferRecord = new SandTransferRecord();
        sandTransferRecord.setUserId(userInfo.getId());

        sandTransferRecord.setAmount(amount);
        sandTransferRecord.setChannel(channel);
        sandTransferRecord.setCharge(amount.multiply(BigDecimal.valueOf(0.002)));
        sandTransferRecord.setApprovalStatus(0);
        sandTransferRecord.setApplyTime(DateTime.now());
        sandTransferRecord.setCustomerOrderNo(OrderNoUtil.getOrderNo());
        sandTransferRecord.insert();
        return sandTransferRecord;
    }

    @Override
    public Page<SandTransferRecord> getPage(Long userId, Date queryTime, int pageNo, int pageSize) {
        return sandTransferRecordDao.getPage(userId, queryTime,pageNo, pageSize);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void backAmount(SandTransferRecord sandTransferRecord) {
        sandTransferRecord.setApprovalStatus(2);
        sandTransferRecord.setWithdrawStatus(2);
        sandTransferRecord.updateById();
        userWalletService.transferFail(sandTransferRecord.getUserId(),sandTransferRecord.getAmount());
    }
}